/***************************/
//@Author: Adrian "yEnS" Mato Gondelle & Ivan Guardado Castro
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/

$(document).ready(function(){
	//global vars
	var form = $("#customForm");
	var name = $("#name");
	var nameInfo = $("#nameInfo");
	var ip = $("#ip");
	var ipInfo = $("#ipInfo");
	var time = $("#time");
	var timeInfo = $("#timeInfo");
	var freq = $("#freq");
	var freqInfo = $("#freqInfo");
	var url = $("#url");
	var urlInfo = $("#urlInfo");
	var texts = $("#texts");
	var textsInfo = $("#textsInfo");
	var num = $("#num");
	var numInfo = $("#numInfo");
	
		
	//On blur
	name.blur(validateName);
	ip.blur(validateIp);
	time.blur(validateTime);
	freq.blur(validateFreq);
	url.blur(validateUrl);
	texts.blur(validateTexts);
	num.blur(validateNum);
	
	//On key press
	name.keyup(validateName);
	ip.keyup(validateIp);
	time.keyup(validateTime);
	freq.keyup(validateFreq);
	url.keyup(validateUrl);
	texts.keyup(validateTexts);
	num.keyup(validateNum);

	//On Submitting
//	form.submit(function(){
//		if(validateHostName() & validateIp() & validateTime())
//			return true
//		else
//			return false;
//	});
	
	//主机名验证
	function validateName(){
		if(name.val().length < 6){
			nameInfo.text("长度过短！请检查。");
			return false;
		}
		else{
			nameInfo.text("通过");
			return true;
		}
	}
	
	//IP验证	
	function validateIp(){
		var a = $("#ip").val();
		var filter = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/
		if(filter.test(a)){
			ipInfo.text("通过");
			return true;
		}
		else{
			ipInfo.text("输入有误，请检查。");
			return false;
		}
	}	
	
	//时间验证	
	function validateTime(){
		var a = $("#time").val();
		var filter = /^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$/
		if(filter.test(a)){
			timeInfo.text("通过");
			return true;
		}
		else{
			timeInfo.text("输入有误，请检查。");
			return false;
		}
	}
	
	//频率验证	
	function validateFreq(){
		var a = $("#freq").val();
		var filter = /^\d+$/ 
		if(!filter.test(a)){
			freqInfo.text("输入有误！请检查。");
			return false;
		}
		if(freq.val() < 60){
			freqInfo.text("输入有误！请检查。");
			return false;
		}
		else{
			freqInfo.text("通过");
			return true;
		}
	}
	
	//URL验证
	function validateUrl(){
		var a = $("#url").val();
		var strRegex = "^((https|http|ftp|rtsp|mms)?://)"
		+ "?(([0-9a-z_!~*'().&amp;=+$%-]+: )?[0-9a-z_!~*'().&amp;=+$%-]+@)?" //ftp的user@
		+ "(([0-9]{1,3}\.){3}[0-9]{1,3}" // IP形式的URL- 199.194.52.184
		+ "|" // 允许IP和DOMAIN（域名）
		+ "([0-9a-z_!~*'()-]+\.)*" // 域名- www.
		+ "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\." // 二级域名
		+ "[a-z]{2,6})" // first level domain- .com or .museum
		+ "(:[0-9]{1,4})?" // 端口- :80
		+ "((/?)|" // a slash isn't required if there is no file name
		+ "(/[0-9a-z_!~*'().;?:@&amp;=+$,%#-]+)+/?)$";
		var filter=new RegExp(strRegex);
		//alert(filter)
		if(!filter.test(a)){
			urlInfo.text("输入有误！请检查。");
			return false;
		}
		else{
			urlInfo.text("通过");
			return true;
		}
	}

	//文本验证
	function validateTexts(){
		if(texts.val().length < 5){
			textsInfo.text("输入有误！请检查。");
			return false;
		}
		else{
			textsInfo.text("通过");
			return true;
		}
	}
	
	//数字验证
	function validateNum(){
		var a = $("#num").val();
		var filter = /^\d+$/ 
		if(!filter.test(a)){
			numInfo.text("输入有误！请检查。");
			return false;
		}
		else{
			numInfo.text("通过");
			return true;
		}
	}
	
});
